<?php


namespace app\common\lib\task;


use app\common\lib\ali\Sms;
use app\common\lib\Redis;
use app\common\lib\redis\Predis;
use Exception;

class Task
{
    /**
     * 异步发送 验证码
     *
     * @param $data
     * @param $serv
     * @return bool
     * @throws Exception
     */
    public function sendSms($data,$serv)
    {
        try{
//            $response = Sms::sendSms($data['phone'],$data['code']);
            $response = "OK";
        }catch (Exception $e){
            return false;
        }
//        if($response->Code == "OK"){
        if($response == "OK"){
            Predis::getInstance()->set(Redis::smsKey($data['phone']), $data['code'], config('redis.out_time'));
        }else{
            return false;
        }
        return true;
    }

    /**
     * 通过Task机智发送赛况实时数据给客户端
     *
     * @param $data
     * @param $serv  swoole的server对象
     * @throws Exception
     */
    public function pushLive($data,$serv)
    {
//        获取到所有连接到的客户端id
        $clients = Predis::getInstance()->sMembers(config("redis.live_game_key"));
        if (!empty($clients)){
            foreach($clients as $fd) {
                $serv->push($fd, json_encode($data));
            }
        }

    }

}